/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.managergen;


import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.bef.bizentity.operation.BizOperation;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.IInternalMgrAction;
import com.inspur.edp.bef.core.be.builtinimpls.BefBuiltInManager;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.bef.dtgenerator.core.mgrImpl.mgrMethods.MgrImpDeleteGenerator;
import com.inspur.edp.bef.dtgenerator.core.mgrImpl.mgrMethods.MgrImpRetrieveGenerator;
import com.inspur.edp.bef.dtgenerator.core.mgrImpl.mgrMethods.MgrImplMethodGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.changeserializer.BefEntityDataChangeSerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.dataserializer.serializer.BefEntityDataSerializerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.field.BefManagerCacheInfoFieldGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.CreateB4QueryDtmAssemblerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.CreateEntityGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetChangeDeserializerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetChangeSerializerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetCreateVarMgrGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetDataDeserializerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetDataSerializerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetManagerCacheInfoGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.GetModelInfosMethodGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.method.createBeforeRetrieveDtmAssembler;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.EntityManagerImplGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.property.BufferManagerPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.property.ChangesetPropGenerator;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntitySerializerItem;
import java.util.ArrayList;
import java.util.List;
import lombok.var;

public class BeMgrClassGenerator extends EntityManagerImplGenerator {

  private BefManagerGenContext befManagerGenContext;

  public BeMgrClassGenerator(BefManagerGenContext parContext) {
    super(parContext);
    this.befManagerGenContext = parContext;
  }

  private BefCoreGenCtx getCoreGEnCtx() {
    return (BefCoreGenCtx) ParentContext.getProjContext();
  }

  @Override
  protected Class getImplParentClassType() {
    return BefBuiltInManager.class;
  }

  @Override
  protected List<ClassMethodGenerator> getMgrActionGenerators() {
    ArrayList<ClassMethodGenerator> generators = new ArrayList<>();
    var actions = getCoreGEnCtx().getBeGenCtx().getBE().getBizMgrActions();
    for (BizOperation action : actions) {
      if (action instanceof IInternalMgrAction) {
        continue;
      }
      BizMgrAction mgrAction = (BizMgrAction) action;
      if (mgrAction == null) {
        continue;
      }
      generators.add(createMgrMethodGenerator(mgrAction));
    }
    for (GspBizEntityObject node : getCoreGEnCtx().getBeGenCtx().getBE().getAllNodes()) {
      if (!node.getIsRootNode()) {
        generators.add(createMgrDeleteMethodGenerator(node));
        generators.add(createMgrRetrieveMethodGenerator(node));
      }
    }
    return generators;
  }

  private ClassMethodGenerator createMgrDeleteMethodGenerator(GspBizEntityObject node) {
    return new MgrImpDeleteGenerator(node);
  }

  private ClassMethodGenerator createMgrRetrieveMethodGenerator(GspBizEntityObject node) {
    return new MgrImpRetrieveGenerator(node);
  }

  private ClassMethodGenerator createMgrMethodGenerator(BizMgrAction action) {
    return new MgrImplMethodGenerator(
        action, getCoreGEnCtx(), getCoreGEnCtx().getBeGenCtx().getMetadataPool());
  }

  @Override
  protected void addExtendEntityMethodGenerator(
      java.util.ArrayList<ClassMethodGenerator> generators) {
    super.addExtendEntityMethodGenerator(generators);
    CreateEntityGenerator createEntityGen = new CreateEntityGenerator(ParentContext);
    generators.add(createEntityGen);
    //var convertGen = new GetConvertorGenerator(ParentContext);
    //generators.Add(convertGen);
//		generators.add(new GetRepoFactoryGenerator());
    generators.add(new CreateB4QueryDtmAssemblerGenerator((BefCoreGenCtx) ParentContext.getProjContext()));
    generators.add(new createBeforeRetrieveDtmAssembler((BefCoreGenCtx) ParentContext.getProjContext()));
    if(befManagerGenContext.getBefCoreGenCtx().getBeGenCtx().hasVariables()) {
      generators.add(new GetCreateVarMgrGenerator(ParentContext));
    }
    generators.add(new GetManagerCacheInfoGenerator());

    //generators.add(new GetChangeSerializerGenerator(befManagerGenContext));
    //generators.add(new GetChangeDeserializerGenerator(befManagerGenContext));
    //generators.add(new GetDataDeserializerGenerator(befManagerGenContext));
    //generators.add(new GetDataSerializerGenerator(befManagerGenContext));
    generators.add(new GetModelInfosMethodGenerator(befManagerGenContext));
  }

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    ParentContext.getProjContext().getCefGenContext().getCoreGenContext()
        .addImport(AbstractEntitySerializerItem.class.getName());
    if (getCoreGEnCtx().getBeGenCtx().getMetadata().getHeader().isExtendable()) {
      befManagerGenContext.getExtendProviders()
          .add(new MediateType("com.inspur.edp.customize.bef", "BefCtmManagerExtend"));
    }
  }

  @Override
  protected String getName() {
    return getCoreGEnCtx().getBE().getCode() + NamePostfix;
  }

  @Override
  protected BufferManagerPropertyGenerator getBufferPropGenerator() {
    return null;
  }

  @Override
  protected ChangesetPropGenerator getChangesetPropGenerator() {
    return null;
  }
  @Override
  protected ArrayList<FieldGenerator> createFieldGenerator() {
    ArrayList<FieldGenerator> list= super.createFieldGenerator();
    if(list == null){
      list = new ArrayList<>();
    }
    list.add(new BefManagerCacheInfoFieldGenerator());
    return list;
  }

  //  @Override
//  protected CefDataSerGenerator getDataSerGenerator()
//  {
//    return new BefEntityDataSerializerGenerator(ParentContext);
//  }
//
//  @Override
//  protected ChangeSerGenerator getChangeSerGenerator() {
//    return new BefEntityDataChangeSerGenerator(ParentContext);
//  }
}
